const sqldb = require('../dbbase/mysqldb');
const db = require('../dbbase/db');

const uid = require("node-uuid");

//获取点检设备点检装置的 名称
exports.getDeviceName = (req, res, next) => {
    try {
        const deviceId = req.body.deviceId;
        const sql = "select mtName deviceName from MT_BASEINFO m where m.id = '" + deviceId + "'";
        const params = [];
        db.query(sql, params).then((result) => {
            res.status(200).json(result)
        }).catch((err) => {
            console.log(err);
        });
    } catch (err) {
        next(err)
    }
}
//获取点检设备点检的 名称
exports.getCheckItemName = async (req, res, next) => {
    try {
        const Ids = req.body;
        let names = [];
        for (let id of Ids) {
            const sql = "select itemName name from LP_CHECK_ITEM m where m.id = '" + id.ID + "'";
            const params = [];
            const name = await db.query(sql, params);
            names.push(name[0])
        }
        res.status(200).json(names)
    } catch (err) {
        next(err)
    }
}
//获取点检设备的点检装置
exports.getDeviceCheckItemList = async (req, res, next) => {
    try {
        const deviceId = req.body.device;
        const ssql = "select DISTINCT PARENTID id from LP_CHECK_ITEM l "
            + "JOIN LP_CHECK_MT_ITEM p on  p.itemid = l.id "
            + "where p.mtid = '" + deviceId + "' "
        db.query(ssql, []).then((result) => {
            res.status(200).json(result)
        }).catch((err) => {
            console.log(err);
        });

    } catch (err) {
        next(err)
    }
}
//获取点检设备点检装置的点检内容
exports.getDeviceCheckContent = (req, res, next) => {
    try {

        const checkId = req.body.checkId;
        const sql = "select * from LP_CHECK_ITEM where parentid='" + checkId + "' ";
        const params = [];
        db.query(sql, params).then((result) => {
            res.status(200).json(result)
        }).catch((err) => {
            console.log(err);
        });
    } catch (err) {
        next(err)
    }
}
//提交 点检内容
exports.postCheckContentList = (req, res, next) => {
    try {
        const checkData = req.body;
        const contentID = uid.v1();
        const sql = "insert into MOV_APP_CHECK_LIST (USERNAME,DEVICENAME,DEVICEID,CHECKNAME,CHECKID,CHECKTIME,CONTENTID) values(:1,:2,:3,:4,:5,:6,:7)";
        const params = [checkData.username, checkData.deviceName, checkData.deviceId, checkData.checkName, checkData.checkId, checkData.checkTime, contentID];

        db.query(sql, params).then((result) => {
            console.log(result);
            res.status(200).json(result);
        }).catch((err) => {
            console.log(err);
        });

        // for (const ele of checkData.checkContent) {
        //     const e = ele.IMGURL;
        //     if(e){
        //          ele.IMGURL = e[0].url;
        //     }else{
        //         ele.IMGURL = ''
        //     }
        //     const sparams = [ele.STANDARD, contentID, ele.MESSAGE, ele.STATUS,ele.IMGURL];
        //     const ssql = "insert into MOV_APP_CHECK_CONTENT (STANDARD,contentID,message,checkState,imgUrl) values(:1,:2,:3,:4,:5)";
        //     db.query(ssql, sparams)
        // }

    } catch (err) {
        next(err)
    }
}
//提交图片
exports.uploadImage = (req, res, next) => {
    try {
        // 返回图片的url
        res.json({
            imageUrl: 'http://127.0.0.1:8080/images/'+ req.body.photo.filename
        })

    } catch (err) {
        next(err)
    }
}


